<?php
/*
 * 1626cms 企業會員中心函數
 * ============================================================================
 * 版權所有: 1626網絡，并保留所有權利。
 * 網站地址: http://www.1626.com；
 * ----------------------------------------------------------------------------
 * 這不是一個自由軟件！您只能在不用于商業目的的前提下對程序代碼進行修改和
 * 使用；不允許對程序代碼以任何形式任何目的的再發布。
 * ============================================================================
*/
if (! defined ( 'IN_QISHI' )) {
	die ( 'Access Denied!' );
}
//******************公共部分
//獲取積分規則
function get_points_rule() {
	global $db;
	$sql = "select * from " . table ( 'members_points_rule' ) . "  ORDER BY id asc";
	$list = $db->getall ( $sql );
	return $list;
}
//獲取會員積分總數
function get_user_points($uid) {
	global $db;
	$sql = "select * from " . table ( 'members_points' ) . " where uid = '$uid'";
	$points = $db->getone ( $sql );
	return $points ['points'];
}
//獲取用戶積分明細類型名稱
function get_user_points_report_tpye($i_type) {
	switch ($i_type) {
		case 1 :
			$type = "<span style=\"color:#009900\">收入</span>";
			break;
		case 2 :
			$type = "<span style=\"color:#FF6600\">支出</span>";
			break;
		default :
			$type = "未知";
	}
	return $type;
}
/* 
積分操作 
$uid:會員ID
$i_type：1：增加積分。2扣除積分
$points:積分
$notes:備注
*/
function report_deal($uid, $i_type = 1, $points = 0, $notes) {
	global $db, $timestamp;
	if ($points == 0)
		return false;
	$points_val = get_user_points ( $uid );
	if ($i_type == 1) {
		$points_val = $points_val + $points;
	}
	if ($i_type == 2) {
		$points_val = $points_val - $points;
		$points_val = $points_val < 0 ? 0 : $points_val;
	}
	$sql = "INSERT INTO " . table ( 'members_points_report' ) . " (uid,i_type,points,remain_points,addtime,notes) VALUES ('$uid','$i_type','$points','$points_val', '$timestamp', '$notes')";
	if (! $db->query ( $sql ))
		return false;
	$sql = "UPDATE " . table ( 'members_points' ) . " SET points= '$points_val' WHERE uid='$uid'";
	if (! $db->query ( $sql ))
		return false;
	return true;
}
//************************************************財務中心
//獲取用戶賬戶明細
function get_user_points_report($offset, $perpage, $get_sql = '') {
	global $db;
	$row_arr = array ();
	if (isset ( $offset ) && ! empty ( $perpage )) {
		$limit = " LIMIT " . $offset . ',' . $perpage;
	}
	$result = $db->query ( "SELECT * FROM " . table ( 'members_points_report' ) . " " . $get_sql . " ORDER BY id DESC " . $limit );
	while ( $row = $db->fetch_array ( $result ) ) {
		$row ['i_type_name'] = get_user_points_report_tpye ( $row ['i_type'] );
		$row ['i_type_li'] = $row ['i_type'] == 1 ? "+" : '-';
		$row_arr [] = $row;
	}
	return $row_arr;
}
//支付方式列表
function get_payment() {
	global $db;
	$sql = "select * from " . table ( 'payment' ) . " where p_install='2' ORDER BY listorder desc";
	$list = $db->getall ( $sql );
	return $list;
}
//獲取支付方式名稱
function get_payment_info($typename, $name = false) {
	global $db;
	$sql = "select * from " . table ( 'payment' ) . " where typename ='" . $typename . "' AND p_install='2'";
	$val = $db->getone ( $sql );
	if ($name) {
		return $val ['byname'];
	} else {
		return $val;
	}
}
//增加充值記錄
function add_payment($uid, $oid, $amount, $payment_name, $points, $addtime) {
	global $db;
	$sql = "INSERT INTO " . table ( 'payment_log' ) . " (id,uid,oid,amount,payment_name,points,addtime) VALUES (NULL,'$uid','$oid','$amount','$payment_name','$points','$addtime')";
	if (! $db->query ( $sql ))
		return false;
	return true;
}
//取消充值記錄
function del_payment($uid, $id) {
	global $db;
	if (! intval ( $uid ) || ! intval ( $id ))
		return false;
	if (! $db->query ( "Delete from " . table ( 'payment_log' ) . " WHERE id='" . $id . "' AND uid=" . $uid . " AND is_paid='1'" ))
		return false;
	return true;
}
//獲取充值記錄列表
function get_payment_all($offset, $perpage, $get_sql = '') {
	global $db;
	$row_arr = array ();
	if (isset ( $offset ) && ! empty ( $perpage )) {
		$limit = " LIMIT " . $offset . ',' . $perpage;
	}
	$result = $db->query ( "SELECT * FROM " . table ( 'payment_log' ) . " " . $get_sql . " ORDER BY id DESC " . $limit );
	while ( $row = $db->fetch_array ( $result ) ) {
		$row ['payment_name'] = get_payment_info ( $row ['payment_name'], true );
		$row_arr [] = $row;
	}
	return $row_arr;
}
//獲取指點會員充值訂單
function get_user_payment($uid, $is_paid) {
	global $db;
	$sql = "select * from " . table ( 'payment_log' ) . " WHERE uid='" . intval ( $uid ) . "' AND  is_paid='" . $is_paid . "' ORDER BY id desc";
	$list = $db->getall ( $sql );
	return $list;
}
//獲取單條充值記錄
function get_payment_one($uid, $id) {
	global $db;
	if (! intval ( $uid ) || ! intval ( $id ))
		return false;
	$sql = "select * from " . table ( 'payment_log' ) . " where id ='$id' AND uid = '" . $uid . "'  AND is_paid ='1'";
	$val = $db->getone ( $sql );
	return $val;
}
//執行充值
function order_paid($v_oid) {
	global $db, $timestamp, $_CFG;
	$sqls = "select * from " . table ( 'payment_log' ) . " WHERE oid ='$v_oid' AND is_paid= '1'";
	$list = $db->getone ( $sqls );
	if ($list) {
		$sql = "UPDATE " . table ( 'payment_log' ) . " SET is_paid= '2',payment_time='" . $timestamp . "' WHERE oid='$v_oid'";
		if (! $db->query ( $sql ))
			return false;
		$notes = date ( 'Y-m-d H:i', time () ) . "通過：" . get_payment_info ( $list ['payment_name'], true ) . " 成功充值 " . $list ['amount'] . "元";
		if (! report_deal ( $list ['uid'], 1, $list ['points'], $notes ))
			return false;
		
		//發送郵件
		$mailconfig = get_cache ( 'mailconfig' );
		if ($mailconfig ['set_payment'] == "1") {
			require_once (QISHI_ROOT_PATH . 'include/fun_user.php');
			asyn_sendmail ( $_CFG ['site_domain'] . $_CFG ['site_dir'] . "asyn_mail.php?uid=" . $list ['uid'] . "&key=" . asyn_userkey ( $list ['uid'] ) . "&act=set_payment" );
		}
		//發送郵件完畢
		return true;
	}
	return true;
}
/////*****************************招聘管理部分
//獲取UID所有已審核職位
function get_user_jobs($uid) {
	global $db;
	$sql = "select * from " . table ( 'jobs' ) . " WHERE uid='" . intval ( $uid ) . "' AND  audit='1'  AND  audit=1 AND  display=1 ORDER BY id desc";
	$list = $db->getall ( $sql );
	return $list;
}
//把簡歷添加到已下載中
function add_down_resume($resume_id, $company_uid, $points) {
	global $db, $timestamp;
	$resumeusql = "select * from " . table ( 'resume' ) . " where id ='" . intval ( $resume_id ) . "'";
	$resume = $db->getone ( $resumeusql ); //獲取簡歷
	if ($resume) {
		$sql = "INSERT INTO " . table ( 'company_down_resume' ) . " (resume_id,resume_uid,company_uid,addtime) VALUES ('" . $resume ['id'] . "','" . $resume ['uid'] . "','" . $company_uid . "','" . $timestamp . "')";
		if (! $db->query ( $sql ))
			return false;
		if (! report_deal ( $company_uid, 2, $points, "下載:" . $resume ['fullname'] . "的個人簡歷扣除：" . $points . "" ))
			return false;
		return true;
	}
	return false;
}
//已下載的簡歷列表
function get_down_resume($offset, $perpage, $get_sql = '') {
	global $db;
	$row_arr = array ();
	if (isset ( $offset ) && ! empty ( $perpage )) {
		$limit = " LIMIT " . $offset . ',' . $perpage;
	}
	$result = $db->query ( "SELECT " . table ( 'resume' ) . ".id," . table ( 'resume' ) . ".uid," . table ( 'company_down_resume' ) . ".did, " . table ( 'resume' ) . ".fullname," . table ( 'resume' ) . ".sex," . table ( 'resume' ) . ".age," . table ( 'resume' ) . ".education," . table ( 'resume' ) . ".experience," . table ( 'company_down_resume' ) . ".addtime ," . table ( 'resume' ) . ".specialty   FROM " . table ( 'company_down_resume' ) . " " . $get_sql . " ORDER BY " . table ( 'company_down_resume' ) . ".did DESC " . $limit );
	while ( $row = $db->fetch_array ( $result ) ) {
		$education = get_jobs_education_one ( $row ['education'] ); //學歷
		$experience = get_jobs_experience_one ( $row ['experience'] ); //工作經驗
		$row ['resume_url'] = url_rewrite ( 'resume-show', array ('id0' => $row ['id'] ) );
		$row ['sex'] = $row ['sex'] == "1" ? "男" : "女";
		$row ['education'] = $education ['categoryname'];
		$row ['experience'] = $experience ['categoryname'];
		$row ['specialty'] = cut_str ( $row ['specialty'], 18, 0 );
		$row_arr [] = $row;
	}
	return $row_arr;
}
//刪除下載的簡歷
function del_down_resume($del_id, $uid) {
	global $db;
	$uidsql = " AND company_uid='" . $uid . "'";
	if (! is_array ( $del_id ))
		return false;
	$sqlin = implode ( ",", $del_id );
	if (! is_numeric ( str_replace ( ",", "", $sqlin ) ))
		return false;
	if (! $db->query ( "Delete from " . table ( 'company_down_resume' ) . " WHERE resume_id IN (" . $sqlin . ") " . $uidsql . "" ))
		return false;
	return true;
}
//添加到人才庫
function add_resume_library($resume_id_arr, $company_uid) {
	global $db, $timestamp;
	if (intval ( $company_uid ) != $company_uid)
		return false;
	if (! is_array ( $resume_id_arr ))
		$resume_id_arr = array ($resume_id_arr );
	if (is_array ( $resume_id_arr )) {
		$sqlin = implode ( ",", $resume_id_arr );
		if (! is_numeric ( str_replace ( ",", "", $sqlin ) ))
			return false;
		foreach ( $resume_id_arr as $resume_id ) {
			if (! check_resume_library ( $resume_id, $company_uid )) {
				$sql = "INSERT INTO " . table ( 'company_resume_library' ) . " (resume_id,company_uid,addtime) VALUES ('$resume_id','$company_uid','$timestamp')";
				if (! $db->query ( $sql ))
					return false;
			}
		}
		return true;
	}
}
//檢測人才庫中是否已經存在
function check_resume_library($resume_id, $company_uid) {
	global $db;
	$sql = "select * from " . table ( 'company_resume_library' ) . " WHERE company_uid = '$company_uid' AND resume_id='$resume_id'";
	$info = $db->getone ( $sql );
	if ($info)
		return true;
	else
		return false;
}
//獲取企業人才庫
function get_resume_library($offset, $perpage, $get_sql = '') {
	global $db;
	$row_arr = array ();
	if (isset ( $offset ) && ! empty ( $perpage )) {
		$limit = " LIMIT " . $offset . ',' . $perpage;
	}
	$result = $db->query ( "SELECT " . table ( 'resume' ) . ".id," . table ( 'resume' ) . ".uid," . table ( 'company_resume_library' ) . ".did, " . table ( 'resume' ) . ".fullname," . table ( 'resume' ) . ".sex," . table ( 'resume' ) . ".age," . table ( 'resume' ) . ".education," . table ( 'resume' ) . ".experience," . table ( 'company_resume_library' ) . ".addtime ," . table ( 'resume' ) . ".specialty   FROM " . table ( 'company_resume_library' ) . " " . $get_sql . " ORDER BY " . table ( 'company_resume_library' ) . ".did DESC " . $limit );
	while ( $row = $db->fetch_array ( $result ) ) {
		$education = get_jobs_education_one ( $row ['education'] ); //學歷
		$experience = get_jobs_experience_one ( $row ['experience'] ); //工作經驗
		$row ['resume_url'] = url_rewrite ( 'resume-show', array ('id0' => $row ['id'] ) );
		$row ['sex'] = $row ['sex'] == "1" ? "男" : "女";
		$row ['education'] = $education ['categoryname'];
		$row ['experience'] = $experience ['categoryname'];
		$row ['specialty'] = cut_str ( $row ['specialty'], 18, 0 );
		$row_arr [] = $row;
	}
	return $row_arr;
}
//刪除 -人才庫中的簡歷
function del_resume_library($del_id, $uid) {
	global $db;
	$uidsql = " AND company_uid='" . $uid . "'";
	if (! is_array ( $del_id ))
		return false;
	$sqlin = implode ( ",", $del_id );
	if (! is_numeric ( str_replace ( ",", "", $sqlin ) ))
		return false;
	if (! $db->query ( "Delete from " . table ( 'company_resume_library' ) . " WHERE did IN (" . $sqlin . ") " . $uidsql . "" ))
		return false;
	return true;
}
//檢測簡歷是否已經下載過
function check_down_resumeid($resume_id, $company_uid) {
	global $db;
	$sql = "select * from " . table ( 'company_down_resume' ) . " WHERE company_uid = '$company_uid' AND resume_id='$resume_id'";
	$info = $db->getone ( $sql );
	if ($info)
		return true;
	else
		return false;
}
//檢測簡歷是否邀請過
function check_invitation_interview($resume_id, $company_uid) {
	global $db;
	$sql = "select * from " . table ( 'company_invitation_interview' ) . " WHERE company_uid = '$company_uid' AND resume_id='$resume_id'";
	$info = $db->getone ( $sql );
	if ($info)
		return true;
	else
		return false;
}
//刪除 -邀請記錄
function del_invitation_interview($del_id, $uid) {
	global $db;
	$uidsql = " AND company_uid='" . $uid . "'";
	if (! is_array ( $del_id ))
		return false;
	$sqlin = implode ( ",", $del_id );
	if (! is_numeric ( str_replace ( ",", "", $sqlin ) ))
		return false;
	if (! $db->query ( "Delete from " . table ( 'company_invitation_interview' ) . " WHERE did IN (" . $sqlin . ") " . $uidsql . "" ))
		return false;
	return true;
}
//邀請記錄列表
function get_invitation_interview($offset, $perpage, $get_sql = '') {
	global $db;
	$row_arr = array ();
	if (isset ( $offset ) && ! empty ( $perpage )) {
		$limit = " LIMIT " . $offset . ',' . $perpage;
	}
	$result = $db->query ( "SELECT " . table ( 'resume' ) . ".id," . table ( 'company_invitation_interview' ) . ".did," . table ( 'company_invitation_interview' ) . ".jobs_name," . table ( 'company_invitation_interview' ) . ".notes," . table ( 'company_invitation_interview' ) . ".personal_look," . table ( 'resume' ) . ".fullname," . table ( 'resume' ) . ".sex," . table ( 'resume' ) . ".age," . table ( 'resume' ) . ".education," . table ( 'resume' ) . ".experience," . table ( 'company_invitation_interview' ) . ".addtime ," . table ( 'resume' ) . ".specialty   FROM " . table ( 'company_invitation_interview' ) . " " . $get_sql . " ORDER BY " . table ( 'company_invitation_interview' ) . ".did DESC " . $limit );
	while ( $row = $db->fetch_array ( $result ) ) {
		$education = get_jobs_education_one ( $row ['education'] ); //學歷
		$experience = get_jobs_experience_one ( $row ['experience'] ); //工作經驗
		$row ['resume_url'] = url_rewrite ( 'resume-show', array ('id0' => $row ['id'] ) );
		$row ['sex'] = $row ['sex'] == "1" ? "男" : "女";
		$row ['education'] = $education ['categoryname'];
		$row ['experience'] = $experience ['categoryname'];
		$row ['specialty'] = cut_str ( $row ['specialty'], 18, 0 );
		$row_arr [] = $row;
	}
	return $row_arr;
}
//獲取申請職位列表
function get_jobs_apply($offset, $perpage, $get_sql = '') {
	global $db;
	$row_arr = array ();
	if (isset ( $offset ) && ! empty ( $perpage )) {
		$limit = " LIMIT " . $offset . ',' . $perpage;
	}
	$result = $db->query ( "SELECT * FROM " . table ( 'personal_jobs_apply' ) . " " . $get_sql . " ORDER BY personal_look ASC " . $limit );
	while ( $row = $db->fetch_array ( $result ) ) {
		$row ['company_url'] = url_rewrite ( 'company', array ('id0' => $row ['company_id'] ) );
		$row ['jobs_url'] = url_rewrite ( 'jobs-show', array ('id0' => $row ['jobs_id'] ) );
		$row_arr [] = $row;
	}
	return $row_arr;
}
//獲取收到的職位申請（單條）,并修改為已經查看
function get_jobs_apply_one($id) {
	global $db;
	if (intval ( $id ) != $id)
		return false;
	$sql = "select * from " . table ( 'personal_jobs_apply' ) . " where did=" . $id;
	$lsit = $db->getone ( $sql );
	$sqlresume = "select * from " . table ( 'resume' ) . " where uid=" . $lsit ['personal_uid'];
	$lsitresume = $db->getone ( $sqlresume );
	$lsit ['resume_url'] = url_rewrite ( 'resume-show', array ('id0' => $lsitresume ['id'] ) );
	$lsit ['personal_sex'] = $lsit ['personal_sex'] == "1" ? "男" : "女";
	$lsit ['jobs_url'] = url_rewrite ( 'jobs-show', array ('id0' => $lsit ['jobs_id'] ) );
	$setsqlarr ['personal_look'] = '2';
	$setsqlarr ['did'] = $id;
	$wheresql = " did='" . $id . "' ";
	updatetable ( table ( 'personal_jobs_apply' ), $setsqlarr, $wheresql );
	unset ( $setsqlarr );
	return $lsit;
}
function del_jobs_shiftwork($job_id) {
	global $db;
	if (! $db->query ( "Delete from " . table ( 'jobs_shiftwork' ) . " WHERE job_id =" . $job_id ))
		return false;
	return true;
}
function del_jobs_welfare($job_id) {
	global $db;
	if (! $db->query ( "Delete from " . table ( 'jobs_welfare' ) . " WHERE job_id =" . $job_id ))
		return false;
	return true;
}
function del_jobs_attachment($job_id, $attachment_dir) {
	global $db;
	require_once (QISHI_ROOT_PATH . 'include/Iohandler.php');
	$iohandler = new Iohandler();
	$sql = "select * from " . table ( 'jobs_attachment' ) . " WHERE job_id =" . $job_id;
	$result = $db->query ( $sql);
	while ( $row = $db->fetch_array ( $result ) ) {
		$iohandler->DeleteFile($attachment_dir.$row ['path']);
	}
	if (! $db->query ( "Delete from " . table ( 'jobs_attachment' ) . " WHERE job_id =" . $job_id ))
		return false;
	return true;
}
function del_jobs_attachment_byuid($id, $user_id, $attachment_dir) {
	global $db;
	$sql = "select * from " . table ( 'jobs_attachment' ) . " WHERE id =" . $id. ' and user_id='.$user_id;
	$info = $db->getone ( $sql );
	if($info)
	{
		$sql = "delete from " . table ( 'jobs_attachment' ) . " WHERE id =" . $id. ' and user_id='.$user_id;
		$db->query ( $sql );
		require_once (QISHI_ROOT_PATH . 'include/Iohandler.php');
		$iohandler = new Iohandler();
		$iohandler->DeleteFile($attachment_dir.$info ['path']);
		return true;
	}
	return false;
}
function get_jobs_attachment($job_id) {
	global $db;
	$sql = "select * from " . table ( 'jobs_attachment' ) . " WHERE job_id =" . $job_id;
	return $db->getall ( $sql );
}
//刪除 -申請職位
function del_jobs_apply($del_id, $uid) {
	global $db;
	$uidsql = " AND company_uid='" . $uid . "'";
	if (! is_array ( $del_id ))
		return false;
	$sqlin = implode ( ",", $del_id );
	if (! is_numeric ( str_replace ( ",", "", $sqlin ) ))
		return false;
	if (! $db->query ( "Delete from " . table ( 'personal_jobs_apply' ) . " WHERE did IN (" . $sqlin . ") " . $uidsql . "" ))
		return false;
	return true;
}
//已發布職位總數
function count_jobs($uid) {
	$wheresql = " WHERE uid='" . $uid . "' ";
	$total_sql = "SELECT COUNT(*) AS num FROM " . table ( 'jobs' ) . $wheresql;
	return get_total ( $total_sql );
}
//已下載簡歷總數
function count_down_resume($uid) {
	$wheresql = " WHERE company_uid='" . $uid . "' ";
	$total_sql = "SELECT COUNT(*) AS num FROM " . table ( 'company_down_resume' ) . $wheresql;
	return get_total ( $total_sql );
}
//收到的職位申請總數
function count_jobs_apply($uid) {
	$wheresql = " WHERE company_uid='" . $uid . "' ";
	$total_sql = "SELECT COUNT(*) AS num FROM " . table ( 'personal_jobs_apply' ) . $wheresql;
	return get_total ( $total_sql );
}
//發起的面試要求總數
function count_invitation_interview($uid) {
	$wheresql = " WHERE company_uid='" . $uid . "' ";
	$total_sql = "SELECT COUNT(*) AS num FROM " . table ( 'company_invitation_interview' ) . $wheresql;
	return get_total ( $total_sql );
}
//人才庫總數
function count_resume_library($uid) {
	$wheresql = " WHERE company_uid='" . $uid . "' ";
	$total_sql = "SELECT COUNT(*) AS num FROM " . table ( 'company_resume_library' ) . $wheresql;
	return get_total ( $total_sql );
}
//獲取會員狀態
function get_user_status($uid) {
	global $db;
	$sql = "select * from " . table ( 'members' ) . " where uid = '$uid'";
	$user_info = $db->getone ( $sql );
	return $user_info ['status'];
}
//修改會員狀態
function set_user_status($status, $uid) {
	global $db;
	if (intval ( $status ) != $status)
		return false;
	$sql = "UPDATE " . table ( 'members' ) . " SET status= '" . intval ( $status ) . "' WHERE uid='" . $uid . "'";
	if (! $db->query ( $sql )) {
		return false;
	} else {
		return true;
	}
}
//用戶反饋列表
function get_feedback($uid) {
	global $db;
	$sql = "select * from " . table ( 'feedback' ) . " where uid='" . $uid . "' ORDER BY id desc";
	$list = $db->getall ( $sql );
	return $list;
}
//刪除用戶反饋
function del_feedback($del_id, $uid) {
	global $db;
	if (intval ( $del_id ) != $del_id)
		return false;
	if (! $db->query ( "Delete from " . table ( 'feedback' ) . " WHERE id='" . intval ( $del_id ) . "' AND uid='" . intval ( $uid ) . "'  " ))
		return false;
	return true;
}
?>
